package topo.service;

import com.zhtelecom.common.base.MyException;
import com.zhtelecom.common.base.MyLog;
import com.zhtelecom.common.snmp.OMMappingInfo;
import com.zhtelecom.common.snmp.SNMPAPI;
import com.zhtelecom.common.snmp.SNMPFactory;
import com.zhtelecom.common.snmp.SNMPTarget;
import java.util.List;
import topo.data.NodeDevice;
import topo.utils.SNMPTargetTool;

public class SNMPBaseObjectService {
    private static SNMPBaseObjectService snmpService = new SNMPBaseObjectService();

    private SNMPAPI snmpAPI = null;

    private SNMPBaseObjectService() {
        try {
            this.snmpAPI = SNMPFactory.getSNMPAPI();
        } catch (Exception ex) {
            MyLog.err("Init SNMP Moudle Erro：" + ex.getMessage());
            MyLog.debug(ex);
        }
    }

    public static SNMPBaseObjectService getInstance() {
        if (snmpService == null) {
            snmpService = new SNMPBaseObjectService();
        }
        return snmpService;
    }

    public void add(OMMappingInfo mibObj) throws MyException {
    }

    public List getTableAll(Class theClass, NodeDevice node) throws MyException {
        try {
            SNMPTarget snmpConninfo = SNMPTargetTool.nodeToSNMPConnInfo(node);
            List list = this.snmpAPI.getAllTableData(theClass, snmpConninfo);
            for (int i = 0; i < list.size(); i++) {
                OMMappingInfo mibObj = (OMMappingInfo) list.get(i);
                mibObj.setNodeName(node.getNodeName());
                mibObj.setDbKey(mibObj.genKey());
            }
            return list;
        } catch (Exception ex) {
            throw new MyException(ex);
        }
    }

    public String getSNMPOIDValue(NodeDevice node, String oid) throws MyException {
        return SNMPTargetTool.getSNMPOIDValue(node, oid);
    }

    public String getSNMPNextOIDValue(NodeDevice node, String oid) throws MyException {
        return SNMPTargetTool.getSNMPNextOIDValue(node, oid);
    }

    public OMMappingInfo refresh(OMMappingInfo mibObj) throws MyException {
        return null;
    }

    public void update(OMMappingInfo mibObj) throws MyException {
    }

    public void delete(OMMappingInfo mibObj) throws MyException {
    }

    public String checkAgentErroMsg(NodeDevice node) {
        return SNMPTargetTool.checkAgentErroMsg(node);
    }

    public static SNMPTarget nodeToSNMPConnInfo(NodeDevice node) {
        return SNMPTargetTool.nodeToSNMPConnInfo(node);
    }

    public static void main(String[] args) {
        try {
            SNMPTarget node = new SNMPTarget();
            node.nodeIP = "192.168.0.120";
            node.readCommunity = "bjjh";
            node.port = 11210;
            node.proxySNMPGateIP = "192.168.6.1:11210";
            SNMPFactory.init(1, "", 0);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}